cooked-waker
cooked_waker provides safe traits for working with [std::task::Waker
][waker] and creating those wakers out of regular, safe Rust structs. It cooks RawWaker
and RawWakerVTable
, making them safe for consumption.
It provides the Wake
and WakeRef
traits, which correspond to the wake
and wake_by_ref
methods on std::task::Waker
, and it provides implenetations of these types for the common reference & pointer types (Arc
, Rc
, &'static
, etc).
Additionally, it provides IntoWaker
, which allows converting any Wake + Clone
type into a Waker
. This trait is automatically derived for any Wake + Clone + Send + Sync + 'static
type.
Basic example
use ;
use ;
use Waker;
static wake_ref_count: AtomicUsize = new;
static wake_value_count: AtomicUsize = new;
static drop_count: AtomicUsize = new;
// A simple Waker struct that atomically increments the relevant static
// counters.
;
assert_eq!;
let waker = StaticWaker;
assert_eq!;
Arc example
use ;
use ;
use Arc;
use Waker;
// A simple struct that counts the number of times it is awoken. Can't
// be awoken by value (because that would discard the counter), so we
// must instead wrap it in an Arc.
let counter_handle = new;
// Create an std::task::Waker
let waker: Waker = counter_handle.clone.into_waker;
waker.wake_by_ref;
waker.wake_by_ref;
let waker2 = waker.clone;
waker2.wake_by_ref;
// Because IntoWaker wrap the pointer directly, without additional
// boxing, we can use will_wake
assert!;
// This calls Counter::wake_by_ref because the Arc doesn't have exclusive
// ownership of the underlying Counter
waker2.wake;
assert_eq!;